#ifndef __Lock_h__
#define __Lock_h__

class LockImpl
{
private:
	void * lHandle;

	LockImpl(const LockImpl&);
	LockImpl& operator=(const LockImpl&);
public:
	LockImpl();
	~LockImpl();

	bool ReadLock();
	bool ReadTryLock();

	bool WriteLock();
	bool WriteTryLock();

	bool UnlockLast();
};

class ReadLock
{
private:
	LockImpl & myImpl;
	bool bIsLocked;

	ReadLock (const ReadLock&);
	ReadLock& operator=(const ReadLock&);
public:
	ReadLock(LockImpl & what, bool withTry = false) : myImpl(what) 
	{ 
		bIsLocked = (withTry ? myImpl.ReadTryLock() : myImpl.ReadLock()); 
	};

	~ReadLock() 
	{ 
		if(bIsLocked) 
			myImpl.UnlockLast(); 
	};

	const bool IsLocked() const { return bIsLocked; };
};

class WriteLock
{
private:
	LockImpl & myImpl;
	bool bIsLocked;

	WriteLock (const WriteLock&);
	WriteLock& operator=(const WriteLock&);
public:
	WriteLock(LockImpl & what, bool withTry = false) : myImpl(what) 
	{ 
		bIsLocked = (withTry ? myImpl.WriteTryLock() : myImpl.WriteLock()); 
	};

	~WriteLock() 
	{ 
		if(bIsLocked) 
			myImpl.UnlockLast(); 
	};

	const bool IsLocked() const { return bIsLocked; };
};



#endif

